package com.ants.boot.system.controller;


import com.ants.boot.core.Response;
import com.ants.boot.core.annotation.Log;
import com.ants.boot.model.BaseController;
import com.ants.boot.system.dto.sysmenu.*;
import com.ants.boot.system.service.SysMenuService;
import com.ants.boot.system.vo.sysmenu.RoleSelectTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


/**
 * @author 蚂蚁会花呗
 * @date 2022/2/24 15:23
 */
@RestController
@RequestMapping("/system/sys_menu")
@Api(tags = "菜单管理")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 获取符合前端的下拉选择框的角色权限菜单
     * @return
     */
    @PostMapping("role_menu_select_tree")
    @Log(module = "菜单信息",operate = "角色下的菜单")
    @ApiOperation(value = "菜单信息",notes = "角色下的菜单")
    public Response roleMenuSelectTree(@RequestBody @Validated RoleIdDto dto){
        RoleSelectTreeVo result = new RoleSelectTreeVo();
        result.setMenus(sysMenuService.selectTreeMenu());
        if (dto.getRoleId() != 0){
            result.setCheckedKeys(sysMenuService.roleCheckMenuIds(dto.getRoleId()));
        }else{
            result.setCheckedKeys(new ArrayList<>());
        }
        return Response.okData(result);
    }

    /**
     * 上级菜单
     * @return
     */
    @GetMapping("menu_select_tree")
    @ApiOperation(value = "上级菜单",notes = "上级菜单树形列表")
    public Response menuSelectTree(){
        return Response.okData(sysMenuService.selectTreeMenu());
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @PostMapping("add_menu")
    @Log(module = "菜单信息",operate = "新增菜单信息")
    @ApiOperation(value = "新增菜单",notes = "新增菜单")
    public Response addMenu(@RequestBody @Validated AddMenuDto dto){
        boolean result = sysMenuService.addMenu(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @PostMapping("update_menu")
    @Log(module = "菜单信息",operate = "更新菜单信息")
    @ApiOperation(value = "更新菜单",notes = "更新菜单")
    public Response updateMenu(@RequestBody @Validated UpdateMenuDto dto){
        boolean result = sysMenuService.updateMenu(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 删除菜单信息
     * @param dto
     * @return
     */
    @PostMapping("del_menu")
    @Log(module = "菜单信息",operate = "删除菜单信息")
    @ApiOperation(value = "删除菜单",notes = "删除菜单")
    public Response delMenu(@RequestBody @Validated DelMenuDto dto){
        boolean result = sysMenuService.delMenu(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 菜单树形列表
     * @return
     */
    @PostMapping("list_info")
    @ApiOperation(value = "菜单树形列表",notes = "菜单树形列表")
    public Response listMenuTree(@RequestBody @Validated QueryMenuDto dto){
        return Response.okData(sysMenuService.listMenuTree(dto));
    }





}
